﻿
@{
    ViewBag.Title = "PurChaseView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";

}
@section head{
    <script src="~/Scripts/exportJs.js"></script>
    }
<div class="layui-tab">
    <ul class="layui-tab-title">
        <li class="layui-this">采购信息</li>
        <li>合同附件</li>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">

            <form class="layui-form" id="FormInfo">
                <div class="layui-form-item">
                    <label class="layui-form-label">主项目选择</label>
                    <div class="layui-input-block">
                        <select name="ProjectId" lay-verify="required" lay-search lay-filter="MaterialSelFilter">
                            <option value=""></option>
                            @for (int i = 0; i < ViewBag.ProjectList.Count; i++)
                            {
                                <option value="@ViewBag.ProjectList[i]["ProjectId"]">@ViewBag.ProjectList[i]["Name"]|@ViewBag.ProjectList[i]["ProjectCode"]|@ViewBag.ProjectList[i]["Type"]</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">产品名称</label>
                    <div class="layui-input-inline" style="width:300px;">
                        <input type="text" name="ProductName" required lay-verify="required" placeholder="输入产品名称" class="layui-input">
                    </div>
                    <label class="layui-form-label">规格型号</label>
                    <div class="layui-input-inline" style="width:300px;">
                        <input type="text" name="Specification" required lay-verify="required" placeholder="输入规格型号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">单位</label>
                    <div class="layui-input-inline" style="width:80px;">
                        <input type="text" name="Unit" required lay-verify="required" placeholder="输入单位" class="layui-input">
                    </div>
                    <label class="layui-form-label">数量</label>
                    <div class="layui-input-inline" style="width:80px;">
                        <input type="number" name="amount" required lay-verify="required" placeholder="输入单位" class="layui-input">
                    </div>
                    <label class="layui-form-label">单价</label>
                    <div class="layui-input-inline" style="width:100px;">
                        <input type="number" name="UnitPrice" required lay-verify="required" placeholder="输入单价" class="layui-input">
                    </div>
                    <label class="layui-form-label">总金额</label>
                    <div class="layui-input-inline" style="width:100px;">
                        <input type="number" name="TotalPrice" required lay-verify="required" placeholder="输入总金额" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-inline" style="width:800px;">
                        <input type="text" name="Memo" placeholder="输入备注（没有可不输入）" class="layui-input">
                    </div>
                    <div class="layui-input-inline">
                        <button type="submit" id="mysubmit" class="layui-btn" lay-submit lay-filter="formVerify" style="display:none;">立即提交</button>
                        <button type="button" class="layui-btn layui-btn-danger" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增</button>
                    </div>
                </div>
            </form>
            <table id="demo1" lay-filter="MaterialTableList"></table>
        </div>
        
        <div class="layui-tab-item">

            <form class="layui-form" enctype="multipart/form-data">
                <div class="layui-form-item">
                    <label class="layui-form-label">主项目选择</label>
                    <div class="layui-input-block">
                        <select name="ProjectId" lay-verify="required" lay-search lay-filter="FileSelFilter" id="UploadProjectId">
                            <option value=""></option>
                            @for (int i = 0; i < ViewBag.ProjectList.Count; i++)
                            {
                                <option value="@ViewBag.ProjectList[i]["ProjectId"]">@ViewBag.ProjectList[i]["Name"]|@ViewBag.ProjectList[i]["ProjectCode"]|@ViewBag.ProjectList[i]["Type"]</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">上传合同</label>
                    <label class="layui-form-label" style="width:250px;">（仅限以下格式：xls|xlsx|doc|docx）</label>
                    <div class="layui-input-block">
                        @*<input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="关键字" class="layui-input" style="width:300px;float:left;">
                    <input type="text" value="" style="display:none" />
                    <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>*@
                        <button type="button" class="layui-btn layui-btn-danger" id="UpLoadExcel">
                            <i class="layui-icon">&#xe67c;</i>上传商品表格
                        </button>
                        <button type="button" style="display:none" id="UpLoadHideBtn"></button>

                    </div>
                </div>
            </form>
            <table id="demo2" lay-filter="FileTableList"></table>
        </div>
    </div>
</div>

<script type="text/template" id="LayerProgBar">
    <div class="layui-progress layui-progress-big" lay-showPercent="true" lay-filter="progressBar">
        <div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
    </div>
</script>

<script>
//注意：选项卡 依赖 element 模块，否则无法进行功能性操作
    layui.use(['form', 'table', 'upload', 'layer','element'], function(){
        var element = layui.element;
        var upload = layui.upload;
        var table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var form = layui.form;

        var IsValidate = 0;
        var LayerIndex;
        element.init();

        form.on('submit(formVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });



        $("#BtnAdd").on("click", function () {
            $("#mysubmit").click();
            if (IsValidate == 1) {
                var obj = $("#FormInfo").serializeArray();
                var data = {};
                $.each(obj, function (i, v) {
                    data[v.name] = v.value;
                });
                //表示新增
                $.ajax({
                    type: "POST",
                    url: "/Purchase/MaterialAdd",
                    async: false,
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify(data),
                    success: function (msg) {
                        if (msg.result == "OK") {
                            layer.msg('新增成功', { icon: 6, time: 1000 });
                            table.reload("demo1");
                            //parent.table.reload("demo");
                            //var index = parent.layer.getFrameIndex(window.name);
                            //parent.layer.close(index);

                            //parent.layui.admin.events.closeThisTabs();
                        } else {
                            alert(msg.result);
                        }
                    }
                });
            }
        });

        form.on('select(MaterialSelFilter)', function (data) {
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象
            if (data.value == "") {
                table.reload("demo1", {
                    where: {
                        SortName: "PurchaseId", Order: "Desc", Condition: " and IsDelete<>1 and 1=0"
                    }
                });
            }
            else {
                table.reload("demo1", {
                    where: {
                        SortName: "PurchaseId", Order: "Desc", Condition: " and IsDelete<>1 and ProjectId=" + data.value
                    }
                });
            }
        });

        form.on('select(FileSelFilter)', function (data) {
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象
            table.reload("demo2", {
                where: {
                    SortName: "FileId", Order: "Desc", Condition: " and IsDelete<>1 and TypeName='Purchase' and ProjectId=" + data.value
                }
            });
        });

        table.render({
            elem: '#demo1'
            , method: 'post'
            //, height: 315
            , url: '/Purchase/MaterialTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PurchaseId", Order: "Desc", Condition: " and IsDelete<>1 and 1=0" }
            , limit: 20
            , limits: [20, 50, 100]
            , cols: [[ //表头
                { field: 'ProductName', title: '产品名称', width: 250 }
                , { field: 'Specification', title: '规格型号', width: 250 }
                , { field: 'Unit', title: '单位', width: 80 }
                , { field: 'amount', title: '数量', width: 80 }
                , { field: 'UnitPrice', title: '单价', width: 100 }
                , { field: 'TotalPrice', title: '总价', width: 100 }
                , { field: 'Memo', title: '备注', width: 200 }
                , {
                    field: "operation", title: "操作", width: 180,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });

        table.on("tool(MaterialTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            //if (layEvent == 'detail') {
            //    parent.layui.index.openTabsPage('/PreEstimate/EquipPriceSingleEditView?EquipPriceId=' + data.EquipPriceId, '安装物料信息编辑');

            //}


            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.ProductName + '，型号:' + data.Specification + '”？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Purchase/MeterialDel",
                            async: false,
                            data: { "PurchaseId": data.PurchaseId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("demo1", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });

        table.render({
            elem: '#demo2'
            , method: 'post'
            //, height: 315
            , url: '/Purchase/FileTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "FileId", Order: "Desc", Condition: " and IsDelete<>1 and 1=0" }
            , limit: 20
            , limits: [20, 50, 100]
            , cols: [[ //表头
                { field: 'Name', title: '文件名', width: 250 }
                , {
                    title: "下载", width: 180,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="Download">下载</a>';
                    }
                }
                , {
                    field: "operation", title: "操作", width: 180,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });

        table.on("tool(FileTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'Download') {
                $.down('/Purchase/Download', 'Path::' + data.Path + "&Name::" + data.Name, 'post');


            }


            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.Name + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Purchase/FilelDel",
                            async: false,
                            data: { "FileId": data.FileId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("demo2", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });




        var uploadInst = upload.render({
            elem: '#UpLoadExcel' //绑定元素
            , url: '/Purchase/Upload/' //上传接口
            , accept: 'file'
            , size: 1024000
            , exts: "xls|xlsx|doc|docx|zip"
            , data: {
                "ProjectId": function () {
                    return $("#UploadProjectId").val();
                }
            }
            , before: function (obj)
            { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                LayerIndex = layer.open({
                    type: 1,
                    title: '进度条',
                    area: ['700px','200'],
                    content: $("#LayerProgBar").html(),
                    closeBtn:0

                });
                //然后需要重新渲染一下上传进度条
                element.init();
            }
		    ,progress: function (e, percent)
            {
                console.log("进度：" + percent + '%');
                element.progress('progressBar', percent + '%');
            }
            , done: function (msg,index,upload) {
                //上传完毕回调
                layer.close(LayerIndex);
                if (msg.result != "OK") {
                    layer.msg(msg.result, { icon: 6, time: 1000 });
                }
                else {
                    layer.msg('上传成功', { icon: 6, time: 1000 });
                    table.reload("demo2", {
                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                    });
                }

            }
            , error: function () {
                //请求异常回调
                alert(msg.result);
            }
        });


    });
</script>

